#include "settings.h"

#include <QDebug>

Settings::Settings(QWidget *parent)
    : QDialog(parent)
{
//    setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);

    setWindowFlag(Qt::FramelessWindowHint);
    setStyleSheet("Settings{border-image:url(:img/background2.jpg)}");
    resize(360, 325);

//      初始化文字标签
    label_1 = new QLabel(this);
    label_1->setText(tr("--设置背景--"));
    label_1->setStyleSheet("QLabel{font-size:20px; font-family:'楷体'; color:white}");
    label_1->resize(200, 30);
    label_1->move(10, 5);
    label_2 = new QLabel(this);
    label_2->setText(tr("--设置大小--"));
    label_2->setStyleSheet("QLabel{font-size:20px; font-family:'楷体'; color:white}");
    label_2->resize(200, 30);
    label_2->move(10, 70);
    label_3 = new QLabel(this);
    label_3->setText(tr("宽度："));
    label_3->setStyleSheet("QLabel{font-size:20px; font-family:'楷体'; color:white}");
    label_3->resize(50, 30);
    label_3->move(10, 110);
    label_4 = new QLabel(this);
    label_4->setText(tr("高度："));
    label_4->setStyleSheet("QLabel{font-size:20px; font-family:'楷体'; color:white}");
    label_4->resize(50, 30);
    label_4->move(10, 145);


//      读取数据（长、宽、状态）
    QSettings openFile("usrInformation.ini", QSettings::IniFormat);
    openFile.setIniCodec(QTextCodec::codecForName("UTF-8"));
    backgroundPic = openFile.value("usr/backgroundPic").toString();
    int ifWindowFixed = openFile.value("usr/windowFixed").toInt();
    int ifWindowOnTop = openFile.value("usr/windowOnTop").toInt();
    int ifWindowBtnDisplay = openFile.value("usr/windowBtnDisplay").toInt();
    int ifWindowAppDisplay = openFile.value("usr/windowAppDisplay").toInt();
    int WindowWidth = openFile.value("usr/WindowWidth").toInt();
    int WindowHeight = openFile.value("usr/WindowHeight").toInt();
    int windowOpacity = openFile.value("usr/windowOpacity").toInt();


//      初始化输入栏
    setWidth = new QLineEdit(this);
    setWidth->setText(tr("%1").arg(WindowWidth));
    setWidth->resize(220, 30);
    setWidth->move(75, 110);
    setWidth->setPlaceholderText("200~1920");
    setWidth->setStyleSheet("border:2px groove gray;border-radius:5px;padding:2px 4px");
    setWidth->setValidator(new MyIntValidator(200, 1920, this));

    setHeight = new QLineEdit(this);
    setHeight->setText(tr("%1").arg(WindowHeight));
    setHeight->resize(220, 30);
    setHeight->move(75, 145);
    setHeight->setPlaceholderText("200~1080");
    setHeight->setStyleSheet("border:2px groove gray;border-radius:5px;padding:2px 4px");
    setHeight->setValidator(new MyIntValidator(200, 1080, this));

    btnSetBackground = new QPushButton(this);
    btnSetBackground->setText(tr("选取背景"));
    btnSetBackground->resize(100, 30);
    btnSetBackground->move(10, 35);
    btnSetBackground->setStyleSheet("QPushButton{color:white; font:bold 17px; background-color:rgb(255, 160, 180); border-radius: 5px; border:none}"
                                    "QPushButton:hover{background-color:rgb(255, 192, 203)}");


//      初始化复选框
    windowFixed = new QCheckBox(tr("窗口固定"), this);
    windowFixed->setStyleSheet("QCheckBox{font-size:20px; font-family:'楷体'; color:white}");
    windowFixed->move(10, 180);
    if (ifWindowFixed) windowFixed->setChecked(true);
    windowOnTop = new QCheckBox(tr("窗口置顶"), this);
    windowOnTop->setStyleSheet("QCheckBox{font-size:20px; font-family:'楷体'; color:white}");
    windowOnTop->move(10, 215);
    if (ifWindowOnTop) windowOnTop->setChecked(true);
    btnDisplay = new QCheckBox(tr("侧边栏离开后消失"), this);
    btnDisplay->setStyleSheet("QCheckBox{font-size:20px; font-family:'楷体'; color:white}");
    btnDisplay->move(150, 180);
    if (!ifWindowBtnDisplay) btnDisplay->setChecked(true);
    appDisplay = new QCheckBox(tr("桌面图标离开后消失"), this);
    appDisplay->setStyleSheet("QCheckBox{font-size:20px; font-family:'楷体'; color:white}");
    appDisplay->move(150, 215);
    if (!ifWindowAppDisplay) appDisplay->setChecked(true);

    pSpinBox = new QSpinBox(this);
    pSpinBox->setMinimum(10);  // 最小值
    pSpinBox->setMaximum(255);  // 最大值
    pSpinBox->setSingleStep(1);  // 步长
    pSpinBox->move(10, 250);

    pSlider = new QSlider(this);
    pSlider->setOrientation(Qt::Horizontal);  // 水平方向
    pSlider->setMinimum(10);  // 最小值
    pSlider->setMaximum(255);  // 最大值
    pSlider->setSingleStep(1);  // 步长
    pSlider->move(100, 250);

//      确认、恢复默认和关闭按钮
    btnOK = new QPushButton(this);
    btnOK->setText(tr("确定（嗯， 嗯）"));
    btnOK->resize(140, 30);
    btnOK->move(10, 285);
    btnOK->setStyleSheet("QPushButton{color:white; font:bold 17px; background-color:rgb(255, 160, 180); border-radius: 5px; border:none}"
                         "QPushButton:hover{background-color:rgb(255, 192, 203)}");
    btnSetDefault = new QPushButton(this);
    btnSetDefault->setText(tr("恢复默认"));
    btnSetDefault->resize(140, 30);
    btnSetDefault->move(210, 285);
    btnSetDefault->setStyleSheet("QPushButton{color:white; font:bold 17px; background-color:rgb(255, 160, 180); border-radius: 5px; border:none}"
                         "QPushButton:hover{background-color:rgb(255, 192, 203)}");
    btnClose = new QPushButton(this);
    btnClose->setStyleSheet("QPushButton{border-image:url(:img/close.png)}"
                            "QPushButton:hover{border-image:url(:img/close-hover.png)}");
    btnClose->resize(30, 30);
    btnClose->move(320, 10);

    connect(btnSetBackground, &QPushButton::clicked, this, &Settings::setBackground);
    connect(btnClose, &QPushButton::clicked, this, &Settings::closeWindow);
    connect(btnOK, &QPushButton::clicked, this, &Settings::OK);
    connect(btnSetDefault, &QPushButton::clicked, this, &Settings::setDefault);
    connect(pSpinBox, SIGNAL(valueChanged(int)), pSlider, SLOT(setValue(int)));
    connect(pSlider, SIGNAL(valueChanged(int)), pSpinBox, SLOT(setValue(int)));
    pSpinBox->setValue(windowOpacity);
}
Settings::~Settings()
{
}

void Settings::setBackground()
{
    backgroundPic = QFileDialog::getOpenFileName(this, "打开图片", ".", "Text(*.png *.jpg *.bmp)");

//    setWidth->setText(QString(QPixmap(backgroundPic).width()));
//    setHeight->setText(QString(QPixmap(backgroundPic).height()));
    if (backgroundPic.isEmpty())
    {
          QMessageBox mesg;
          mesg.warning(this, "警告", "打开图片失败!");
          return;
     }
     else
     {
        int w = QPixmap(backgroundPic).width();
        int h = QPixmap(backgroundPic).height();
        setWidth->setText(tr("%1").arg(w));
        setHeight->setText(tr("%1").arg(h));
     }
}
void Settings::OK()
{
    if (setWidth->text().trimmed() != "" &&
        setHeight->text().trimmed() != "")
    {
        emit emitBackgroundPic(backgroundPic.isEmpty(), backgroundPic);
        emit emitWindowFixed(windowFixed->isChecked());
        emit emitWindowOnTop(windowOnTop->isChecked());
        emit emitSize(setWidth->text().trimmed().toInt(), setHeight->text().toInt());
        emit emitBtnAndAppDisplay(btnDisplay->isChecked(), appDisplay->isChecked());
        emit emitOpacity(pSlider->value());
        reject();
    }
    else QMessageBox::warning(this, "警告", "交白卷咯", QMessageBox::Yes);
}
void Settings::setDefault()
{
    QMessageBox mesg;
    QMessageBox:: StandardButton result = mesg.information(this, "em...", "确定吗", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if (result == QMessageBox::Yes)
    {
        emit emitSetDefault();
        reject();
    }
}

/**
 * 基本设置
 */

void Settings::closeWindow()
{
    reject();
}
void Settings::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_move = true;
        m_startPoint = event->globalPos();
        m_windowPoint = this->frameGeometry().topLeft();
    }
}
void Settings::mouseMoveEvent(QMouseEvent *event)
{
    if (m_move)
    {
        QPoint relativePos = event->globalPos() - m_startPoint;
        this->move(m_windowPoint + relativePos);
    }
}
void Settings::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_move = false;
    }
}
